import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Student } from './entities/student.entity';
import { Repository } from 'typeorm';
import { generateUniqueId } from 'src/utils/upique_id';
import { CreateStudentDto } from './dto/create-student.dto';
import { UpdateStudentDto } from './dto/update-student.dto';
import { Classroom } from './entities/classroom.entity';

@Injectable()
export class StudentService {
  constructor(
    @InjectRepository(Student)
    private readonly studentRepository: Repository<Student>,
    @InjectRepository(Classroom)
    private readonly classroomRepository: Repository<Classroom>,
  ) {}

  async create(student: CreateStudentDto): Promise<any> {
    const classroom = await this.classroomRepository.findOne({
      where: { class_id: student.class_id },
    });
    if (!classroom) {
      throw new BadRequestException("没找到这个班级");
    }
    student.classroom = classroom;
    console.log('student',student)
    await this.studentRepository.save({
      ...student,
      student_id: `student_${generateUniqueId(10)}`,
    });
    return {
      status: 200,
      message: '学生创建成功',
    };
  }
}
